package br.ufes.ceunes.poo.view.consultas;


import br.ufes.ceunes.poo.model.dao.DAOGenerico;
import br.ufes.ceunes.poo.model.dao.ProdutoDAO;
import br.ufes.ceunes.poo.model.pojo.Produto;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

public class Produtos extends JFrame {

    private DAOGenerico dao;
    public Produtos() {
        initComponents();
        dao = new ProdutoDAO();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        bModoBusca = new javax.swing.JComboBox();
        tTitulo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tProduto = new javax.swing.JTable();
        txInfo = new javax.swing.JTextField();
        tInformacao = new javax.swing.JLabel();
        bBuscar = new javax.swing.JButton();
        bAlterarProduto = new javax.swing.JButton();

        jButton1.setText("jButton1");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane2.setViewportView(jTextArea1);

        jSeparator1.setName("jSeparator1"); // NOI18N

        bModoBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ID", "Nome" }));
        bModoBusca.setName("bModoBusca"); // NOI18N
        bModoBusca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bModoBuscaActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tTitulo.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tTitulo.setText("Busca de Produto");
        tTitulo.setName("tTitulo"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tProduto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID", "Nome", "Valor", "Estoque"
            }
        ));
        tProduto.setName("tProduto"); // NOI18N
        tProduto.setShowHorizontalLines(false);
        tProduto.setShowVerticalLines(false);
        jScrollPane1.setViewportView(tProduto);

        txInfo.setText("Nome");
        txInfo.setName("txInfo"); // NOI18N
        txInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txInfoActionPerformed(evt);
            }
        });

        tInformacao.setText("Entre com o nome a ser buscado:");
        tInformacao.setName("tInformacao"); // NOI18N

        bBuscar.setText("Buscar");
        bBuscar.setName("bBuscar"); // NOI18N
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        bAlterarProduto.setText("Alterar produto");
        bAlterarProduto.setName("bAlterarProduto"); // NOI18N
        bAlterarProduto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAlterarProdutoActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(188, 188, 188)
                .add(bAlterarProduto)
                .add(0, 0, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(161, 161, 161)
                .add(tTitulo)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(tInformacao)
                        .add(35, 35, 35)
                        .add(txInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(bBuscar))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(tTitulo)
                .add(38, 38, 38)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(tInformacao)
                    .add(bBuscar))
                .add(30, 30, 30)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 35, Short.MAX_VALUE)
                .add(bAlterarProduto)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txInfoActionPerformed
        txInfo.setText("");
    }//GEN-LAST:event_txInfoActionPerformed

    private void bModoBuscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bModoBuscaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bModoBuscaActionPerformed

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        String info = txInfo.getText();
        Integer opcao = bModoBusca.getSelectedIndex();
        if (opcao == 0) {
            preencherTabela(((ProdutoDAO)dao).buscaPorNome(info));
        }
        
    }//GEN-LAST:event_bBuscarActionPerformed

    private void bAlterarProdutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAlterarProdutoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bAlterarProdutoActionPerformed

    public static void main(String args[]) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Produtos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Produtos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Produtos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Produtos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Produtos().setVisible(true);
            }
        });
    }
    private void preencherTabela(List<Produto> produtos) {
        DefaultTableModel model = (DefaultTableModel) tProduto.getModel();  
        model.setRowCount(0);
        for (Produto p : produtos) {
            model.addRow(new Object[] {null,null,null,null});
            model.setValueAt(p.getId(), model.getRowCount()-1, 0);
            model.setValueAt(p.getNome(), model.getRowCount()-1, 1);
            model.setValueAt(p.getPreco(), model.getRowCount()-1,2);
            model.setValueAt(p.getEstoque(), model.getRowCount()-1,3);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAlterarProduto;
    private javax.swing.JButton bBuscar;
    private javax.swing.JComboBox bModoBusca;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel tInformacao;
    private javax.swing.JTable tProduto;
    private javax.swing.JLabel tTitulo;
    private javax.swing.JTextField txInfo;
    // End of variables declaration//GEN-END:variables

}
